// 02login/login.js
var co = require('../resource/js/common.js')
var M = require('../resource/js/md5.js')
var app = getApp()
var _this, _data
var interval = null //倒计时函数

Page({
   /*页面的初始数据*/
   data: {
      isThis: 0, //切换登录方式
      isUserName: '', // 登录方式错误提示信息
      isPassWord: '',
      isPhoneNumber: '',
      isVerificationCode: '',
      time: '获取验证码', // 短信验证码
      currentTime: 60,
      smsCode: '',
      phoneNumber: '', // 输入的手机号
      tempPhone: '',
      userInfo: '',
      erroInfo: '', // 提示
      isShowReg: false, // 商家注册按钮显示
      loginInfo: ''
   },

   /* 生命周期函数--监听页面加载*/
   onLoad(opt) {
      _this = this
      _data = this.data

      _this.setData({
         loginInfo: wx.getStorageSync('loginInfo'),
         login_phoneNumber: wx.getStorageSync('login_phoneNumber'),
      })

      //转发获取群信息
      wx.showShareMenu({
         withShareTicket: true
      })

      if (opt)
         if (opt.type)
            if (opt.type == 'logout')
               return

      var session = wx.getStorageSync('session')
      if (!co.empty(session.unionid) || !co.empty(session.openid))
         {
            //this.loginBizOrWaiter(session)
            }else
         this.getWxOpenInfo()
   },

   //再次调用微信授权
   getWxOpenInfo(e) {
      var session = wx.getStorageSync('session')
      if (co.empty(session.session_key)) {
         app.wxLogin(function(res) {
            console.log("【app.wxLogin登录结果】===", res)
            _this.loginBizOrWaiter(res)
         })
         return
      }
      //由onLoad调用时，e为空 或 用户拒绝时，不往下执行
      if (co.empty(e))
         return
      app.decryptWxInfoData(e.detail, function(res) {
         _this.loginBizOrWaiter(res)
      })
   },

   // 商家或服务员自动登录
   loginBizOrWaiter(obj) {
      co.getShareApi('user/Mg/autoLogin', {
         unionid: obj.unionid,
         openid: obj.openid, //因为换appid，需要更新Wx_Openid
         appid: app.Data.app.appid //因为换appid，需要更新Wx_Openid
      }, function(res) {
         var D = res.data
         //if (res.code == 0) {
         // 微信审核暂改为132，审核过了，改回0
         if (res.code == 132) {
            _this.goto(res.data)
            return
         }
         //else if (res.code == 29300 || res.code == 29311) {
         //微信审核暂改为132，审核过了，改回29300,29311
         else if (res.code == 13200 || res.code == 13211) {
            wx.switchTab({
               url: '/01wasteRecovery/wasteRecovery'
            })
         }
         // _this.setData({ erroInfo: res.msg })
      })
   },

   goto(D) {
      app.Data.userInfo = D.userInfo
      wx.setStorageSync('unionid', D.userInfo.unionid)
      if (D.biz_list.length == 1) {
         this.gotoShopHome(D.biz_list[0])
         console.log('D.biz_list[0]==', D.biz_list[0])
         return
      }
      var index = wx.getStorageSync('shopIndex')
      if (index) {
         this.gotoShopHome(D.biz_list[index])
         console.log('D.biz_list[index]==', D.biz_list[index])
         return
      }

      var url = '/01wasteRecovery/wasteRecovery'
      wx.switchTab({
         // url: url + JSON.stringify(D.biz_list),
         url: url
      })
   },

   // 进入首页
   gotoShopHome(shopInfo) {
      app.Data.shopInfo = shopInfo
      if (shopInfo.Biz_Type == 'p_org' || shopInfo.Biz_Type == 'amtf') {
         var url = '/01wasteRecovery/wasteRecovery'
         // url += '?Biz_ID=' + shopInfo.Biz_ID
         wx.switchTab({
            url: url,
         })
      } else {
         app.switchTabHome()
      }
   },

   // 获取手机号
   getPhoneNumber(e) {
      var phone = e.detail.value //输入的手机号码存入
      this.setData({
         phoneNumber: phone
      })
   },

   // 获取验证码
   getVerCode() {
      var inPhone = _data.phoneNumber //输入手机号
      var telRule = /^1[0-9]{10}$/ //手机号码格式
      var isPhoneNumber = ''
      if (inPhone == '') {
         co.toastAllText("请输入手机号码!")
      } else if (!telRule.test(inPhone)) {
         isPhoneNumber = '手机号码格式错误'
      } else {
         this.getCode();
         this.setData({
            disabled: true
         })
         co.getShareApi('api/Sms/getPhoneCode', {
            mobile: _data.phoneNumber,
            type: 'quick_login'
         }, function(res) {
            // console.log("【获取返回数据】===", res.data)
            _this.setData({
               smsCode: res.data.code
            })
         })
      }
      this.setData({
         isPhoneNumber: isPhoneNumber,
         tempPhone: inPhone
      })
   },

   // 确认登录
   confirmLogin(e) {
      // console.log("【form携带值为】===", e.detail.value)
      var et = e.detail.value //form携带值为

      if (co.empty(et.phoneNumber))
         wx.setStorageSync('loginInfo', et)
      else {
         wx.setStorageSync('login_phoneNumber', et.phoneNumber)
      }

      var id = _data.isThis //登录方式id
      var smsCode = _data.smsCode //获取到的短信验证码
      var tmPhone = _data.tempPhone //获取验证码的手机码
      var USN = et.userName //form提交用户名
      var PSW = et.passWord //form提交密码
      var PHN = et.phoneNumber //from提交手机号
      var VCO = et.verificationCode //from提交验证码
      var isUserName = ''
      var isPassWord = ''
      var isPhoneNumber = ''
      var isVerificationCode = ''
      if (id == 0) {
         if (USN == '') {
            isUserName = '用户名必填'
         }
         if (PSW == '') {
            isPassWord = '密码必填'
         }
         if (USN == '' || PSW == '') {
            this.setData({
               isUserName: isUserName,
               isPassWord: isPassWord
            })
            return
         }
         var param = {
            Login_Name: USN,
            Login_Pass: M.md5(PSW)
         }
         this.Login(param)
      } else {
         if (PHN == '') {
            isPhoneNumber = '手机号码必填'
         }
         if (VCO == '') {
            isVerificationCode = '验证码必填'
         }
         if (PHN == '' || VCO == '') {
            this.setData({
               isPhoneNumber: isPhoneNumber,
               isVerificationCode: isVerificationCode
            })
            return
         }
         // 判断验证码是否相等
         if (VCO != smsCode) {
            isVerificationCode = '验证码错误'
         } else if (tmPhone != PHN) {
            isVerificationCode = '验证码已过期'
         } else {
            var param = {
               User_Mobile: PHN,
               Sms_Code: VCO
            }
            this.Login(param)
         }
         this.setData({
            isVerificationCode: isVerificationCode
         })
      }
   },

   // 登录
   Login(param) {
      var isUserName = '',
         isPassWord = '',
         sPhoneNumber = ''
      // 账号密码登录
      co.getShareApi('user/Mg/checkLogin', param, function(res) {
         // console.log("【登录成功返回数据】===",res.data)
         if (res.code == 29300 || res.code == 13200) {
            isUserName = res.msg
         }
         if (res.code == 29375) {
            isPassWord = res.msg
         }

         if (res.code == 0 || res.code == 132) {
            _this.goto(res.data)
         } else if (res.code == 29300 || res.code == 29311) {
            wx.switchTab({
               url: '/01wasteRecovery/wasteRecovery'
            })
         }

         _this.setData({
            isUserName: isUserName,
            isPassWord: isPassWord,
            isPhoneNumber: isUserName
         })
      })
   },

   // 点击切换登录
   changeContent(e) {
      this.setData({
         isThis: e.currentTarget.id
      })
   },

   /*获取短信验证码倒计时*/
   getCode(options) {
      var currentTime = _data.currentTime
      interval = setInterval(function() {
         currentTime--;
         _this.setData({
            time: currentTime + '秒'
         })
         if (currentTime <= 0) {
            clearInterval(interval)
            _this.setData({
               time: '重新发送',
               currentTime: 120,
               disabled: false,
               smsCode: '',
            })
         }
      }, 1000)
   },

   //分享
   onShareAppMessage: function() {
      var urlParam = ''
      if (app.Data.userInfo.User_ID) {
         urlParam = '?scene=InviterID.' + app.Data.userInfo.User_ID + '.BizType.meal'
      }
      // var timestamp = Date.parse(new Date()) 
      return {
         title: '[码上点]邀您登入,前100名免入驻费!顾客自助点菜,预约订桌点餐、外卖，让您轻松管理餐饮业',
         path: '/01login/login' + urlParam,
         imageUrl: 'http://h5.52g3.cc/h5/invite/invite_image.png',
         success: function(res) {
            console.log("【分享成功res】===", res)
            var pages = getCurrentPages() //获取加载的页面
            var currentPage = pages[pages.length - 1] //获取当前页面的对象
            app.getShareInfo(res, '邀请商家', currentPage.route)
         }
      }
   },
  
})